package com.example.demoapi.api.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demoapi.api.mapper.DetailsMapper;
import com.example.demoapi.api.mapper.InfoMappingMapper;
import com.example.demoapi.api.mapper.OrderMapper;
import com.example.demoapi.api.pojo.Details;
import com.example.demoapi.api.pojo.InfoMapping;
import com.example.demoapi.api.pojo.Orders;
import com.example.demoapi.api.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Hu
 * @date 2023/2/6
 * @apiNote
 */
@Service
public class DetailsServiceImpl extends ServiceImpl<DetailsMapper, Details> implements DetailsService{

    @Autowired
    private DetailsMapper detailsMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private InfoMappingMapper infoMappingMapper;
    @Override
    public Result queryUserInfo(int userId) {
        QueryWrapper<Details> queryWrapper = new QueryWrapper<Details>().eq("user_id", userId).orderByDesc("create_time");
        List<Details> selectList = detailsMapper.selectList(queryWrapper);
        for (Details details : selectList) {
            InfoMapping infoMapping = infoMappingMapper.selectOne(
                    new QueryWrapper<InfoMapping>().eq("info_mapping", details.getInfo()));
            details.setInfoMapping(infoMapping);
            Orders orders = orderMapper.selectOne(
                    new QueryWrapper<Orders>().eq("order_id", details.getOrderId()));
            details.setOrders(orders);
        }
        return Result.success(selectList);
    }
}
